using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using Antlr4.StringTemplate;
using Antlr.Runtime;


namespace NCS_CS
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormHtmlEditorMethod : System.Windows.Forms.Form
	{
		private AxSHDocVw.AxWebBrowser axWebBrowser1;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.ToolBarButton toolBarButton3;
		private System.Windows.Forms.ToolBarButton toolBarButton4;
		private System.Windows.Forms.ToolBarButton toolBarButton5;
		private System.Windows.Forms.ToolBarButton toolBarButton6;
		private System.Windows.Forms.ToolBarButton toolBarButton7;
		private System.Windows.Forms.ToolBarButton toolBarButton8;
		private System.Windows.Forms.ToolBarButton toolBarButton9;
		private System.Windows.Forms.ToolBarButton toolBarButton10;
		private System.Windows.Forms.ToolBarButton toolBarButton11;
		private System.Windows.Forms.ToolBarButton toolBarButton12;
		private System.Windows.Forms.ToolBarButton toolBarButton13;
		private System.Windows.Forms.ToolBarButton toolBarButton14;
		private System.Windows.Forms.ToolBarButton toolBarButton15;
		private System.Windows.Forms.ToolBarButton toolBarButton16;
		private System.Windows.Forms.ToolBarButton toolBarButton17;
		private System.Windows.Forms.ToolBarButton toolBarButton18;
		private System.Windows.Forms.ToolBarButton toolBarButton19;
		private System.Windows.Forms.ToolBarButton toolBarButton20;
		private System.Windows.Forms.ToolBarButton toolBarButton21;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.TextBox textBox1;
		private System.ComponentModel.IContainer components;

		public FormHtmlEditorMethod()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormHtmlEditorMethod));
            this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
            this.toolBar1 = new System.Windows.Forms.ToolBar();
            this.toolBarButton21 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton17 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton19 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton18 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton8 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton9 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton10 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton20 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton11 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton12 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton13 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton14 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton15 = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton16 = new System.Windows.Forms.ToolBarButton();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.textBox1 = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.SuspendLayout();
            // 
            // axWebBrowser1
            // 
            this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.axWebBrowser1.Enabled = true;
            this.axWebBrowser1.Location = new System.Drawing.Point(0, 28);
            this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
            this.axWebBrowser1.Size = new System.Drawing.Size(640, 416);
            this.axWebBrowser1.TabIndex = 0;
            this.axWebBrowser1.Enter += new System.EventHandler(this.axWebBrowser1_Enter);
            // 
            // toolBar1
            // 
            this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
            this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
            this.toolBarButton21,
            this.toolBarButton1,
            this.toolBarButton17,
            this.toolBarButton3,
            this.toolBarButton4,
            this.toolBarButton5,
            this.toolBarButton19,
            this.toolBarButton6,
            this.toolBarButton7,
            this.toolBarButton18,
            this.toolBarButton8,
            this.toolBarButton9,
            this.toolBarButton10,
            this.toolBarButton20,
            this.toolBarButton11,
            this.toolBarButton12,
            this.toolBarButton13,
            this.toolBarButton14,
            this.toolBarButton15,
            this.toolBarButton16});
            this.toolBar1.DropDownArrows = true;
            this.toolBar1.ImageList = this.imageList1;
            this.toolBar1.Location = new System.Drawing.Point(0, 0);
            this.toolBar1.Name = "toolBar1";
            this.toolBar1.ShowToolTips = true;
            this.toolBar1.Size = new System.Drawing.Size(640, 28);
            this.toolBar1.TabIndex = 1;
            this.toolBar1.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
            this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
            // 
            // toolBarButton21
            // 
            this.toolBarButton21.ImageIndex = 10;
            this.toolBarButton21.Name = "toolBarButton21";
            this.toolBarButton21.ToolTipText = "Open";
            // 
            // toolBarButton1
            // 
            this.toolBarButton1.ImageIndex = 0;
            this.toolBarButton1.Name = "toolBarButton1";
            this.toolBarButton1.ToolTipText = "Save";
            // 
            // toolBarButton17
            // 
            this.toolBarButton17.Name = "toolBarButton17";
            this.toolBarButton17.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // toolBarButton3
            // 
            this.toolBarButton3.ImageIndex = 2;
            this.toolBarButton3.Name = "toolBarButton3";
            this.toolBarButton3.ToolTipText = "Cut";
            // 
            // toolBarButton4
            // 
            this.toolBarButton4.ImageIndex = 3;
            this.toolBarButton4.Name = "toolBarButton4";
            this.toolBarButton4.ToolTipText = "Copy";
            // 
            // toolBarButton5
            // 
            this.toolBarButton5.ImageIndex = 4;
            this.toolBarButton5.Name = "toolBarButton5";
            this.toolBarButton5.ToolTipText = "Paste";
            // 
            // toolBarButton19
            // 
            this.toolBarButton19.Name = "toolBarButton19";
            this.toolBarButton19.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // toolBarButton6
            // 
            this.toolBarButton6.ImageIndex = 6;
            this.toolBarButton6.Name = "toolBarButton6";
            this.toolBarButton6.ToolTipText = "Undo";
            // 
            // toolBarButton7
            // 
            this.toolBarButton7.ImageIndex = 5;
            this.toolBarButton7.Name = "toolBarButton7";
            this.toolBarButton7.ToolTipText = "Redo";
            // 
            // toolBarButton18
            // 
            this.toolBarButton18.Name = "toolBarButton18";
            this.toolBarButton18.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // toolBarButton8
            // 
            this.toolBarButton8.ImageIndex = 7;
            this.toolBarButton8.Name = "toolBarButton8";
            this.toolBarButton8.ToolTipText = "Bold";
            // 
            // toolBarButton9
            // 
            this.toolBarButton9.ImageIndex = 8;
            this.toolBarButton9.Name = "toolBarButton9";
            this.toolBarButton9.ToolTipText = "Italic";
            // 
            // toolBarButton10
            // 
            this.toolBarButton10.ImageIndex = 9;
            this.toolBarButton10.Name = "toolBarButton10";
            this.toolBarButton10.ToolTipText = "Underline";
            // 
            // toolBarButton20
            // 
            this.toolBarButton20.Name = "toolBarButton20";
            this.toolBarButton20.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // toolBarButton11
            // 
            this.toolBarButton11.Name = "toolBarButton11";
            this.toolBarButton11.Text = "1";
            this.toolBarButton11.ToolTipText = "Font Size";
            // 
            // toolBarButton12
            // 
            this.toolBarButton12.Name = "toolBarButton12";
            this.toolBarButton12.Text = "2";
            this.toolBarButton12.ToolTipText = "Font Size";
            // 
            // toolBarButton13
            // 
            this.toolBarButton13.Name = "toolBarButton13";
            this.toolBarButton13.Text = "3";
            this.toolBarButton13.ToolTipText = "Font Size";
            // 
            // toolBarButton14
            // 
            this.toolBarButton14.Name = "toolBarButton14";
            this.toolBarButton14.Text = "4";
            this.toolBarButton14.ToolTipText = "Font Size";
            // 
            // toolBarButton15
            // 
            this.toolBarButton15.Name = "toolBarButton15";
            this.toolBarButton15.Text = "5";
            // 
            // toolBarButton16
            // 
            this.toolBarButton16.Name = "toolBarButton16";
            this.toolBarButton16.Text = "6";
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "");
            this.imageList1.Images.SetKeyName(1, "");
            this.imageList1.Images.SetKeyName(2, "");
            this.imageList1.Images.SetKeyName(3, "");
            this.imageList1.Images.SetKeyName(4, "");
            this.imageList1.Images.SetKeyName(5, "");
            this.imageList1.Images.SetKeyName(6, "");
            this.imageList1.Images.SetKeyName(7, "");
            this.imageList1.Images.SetKeyName(8, "");
            this.imageList1.Images.SetKeyName(9, "");
            this.imageList1.Images.SetKeyName(10, "");
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.Title = "Open HTML File";
            this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
            // 
            // saveFileDialog1
            // 
            this.saveFileDialog1.Title = "Save HTML File";
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(648, 469);
            this.tabControl1.TabIndex = 2;
            this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.axWebBrowser1);
            this.tabPage1.Controls.Add(this.toolBar1);
            this.tabPage1.Location = new System.Drawing.Point(4, 21);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(640, 444);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Editor View";
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.textBox1);
            this.tabPage2.Location = new System.Drawing.Point(4, 21);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(640, 444);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Source View";
            // 
            // textBox1
            // 
            this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(640, 444);
            this.textBox1.TabIndex = 0;
            // 
            // FormHtmlEditorMethod
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(648, 469);
            this.Controls.Add(this.tabControl1);
            this.Name = "FormHtmlEditorMethod";
            this.Text = "Method HTML Report";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.tabPage2.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
        //[STAThread]
        //static void Main() 
        //{
        //    Application.Run(new FormHtmlEditorMethod());
        //}

		private void Form1_Load(object sender, System.EventArgs e)
		{
            string strtemplate;
            //System.IO.StringReader r = new StringReader(modLog.initpath+"\\method.htm");
            System.IO.StreamReader r = new StreamReader(modLog.initpath + "\\method.stg", System.Text.Encoding.UTF8);
            strtemplate = r.ReadToEnd();
            r.Close();
            //this.tabControl1.SelectedIndex = 1;
            //Template st = new Template(strtemplate, '$', '$');
            ////TemplateGroup g = new TemplateGroup('$', '$');
            //st.Add("method", ModMain.method1);
            //string strhtml;
            //this.textBox1.Text = strhtml = st.Render(); // "<b>999</b>: parrt"

            //object path = Directory.GetCurrentDirectory() + "\\method.htm";
            //object missing = System.Reflection.Missing.Value;
            //this.axWebBrowser1.Navigate2(ref path, ref missing, ref missing, ref missing, ref missing);
            //this.axWebBrowser1.Document = strhtml;
            //mshtml.HTMLDocumentClass doc;
            //doc = (mshtml.HTMLDocumentClass)this.axWebBrowser1.Document;
            //doc.designMode = "On";		// This turns the control into an editor
            //TemplateGroup g = new TemplateGroup('$', '$');
            //g.Encoding = System.Text.Encoding.UTF8; 
            ////g.LoadAbsoluteTemplateFile(modLog.initpath+"\\method.htm") ;
            //string filename = modLog.initpath + "\\method.htm";
            //g.LoadTemplateFile("", filename, new ANTLRFileStream(filename, System.Text.Encoding.UTF8));

            //Template t=g.GetInstanceOf("showMethod");
            //t.Add("method", ModMain.method1);
            //this.textBox1.Text = t.Render();
            TemplateGroupString g = new TemplateGroupString("g1",strtemplate,'$','$');
            Template t = g.GetInstanceOf("showMethod");
            g.RegisterRenderer(typeof(double),new DoubleRender()); 
            
            t.Add("method", ModMain.method1);
            this.textBox1.Text = t.Render();
            System.IO.StreamWriter  w=new StreamWriter(modLog.initpath + "\\method.htm");//, System.Text.Encoding.UTF8);
            w.Write(this.textBox1.Text);
            w.Close();
            object path = modLog.initpath + "\\method.htm";
            object missing = System.Reflection.Missing.Value;
            this.axWebBrowser1.Navigate2(ref path, ref missing, ref missing, ref missing, ref missing);

		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)this.axWebBrowser1.Document;
			object empty = null;
			switch (e.Button.ImageIndex)
			{
				case 10:		// Open
					this.openFileDialog1.ShowDialog();
					break;

				case 0:			// Save
                    //this.saveFileDialog1.Title = "Export to " + type.ToUpper();
                    this.saveFileDialog1.Filter = "Web Page (*.html, *.htm)|*.html;*.htm|All files (*.*)|*.*";
                    this.saveFileDialog1.FilterIndex = 1;
                    this.saveFileDialog1.FileName = ModMain.method1.cname;
                    if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filename = this.saveFileDialog1.FileName;
                        StreamWriter file = new StreamWriter(filename, false, System.Text.Encoding.UTF8 );
                        //mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)this.axWebBrowser1.Document;
                        string str =  "<html>"+doc.body.parentElement.innerHTML+"</html>";
                        file.Write(str);
                        file.Close();
                    }
					break;

				case 2:		// Cut
					doc.execCommand("Cut" ,false, empty);
					break;

				case 3:		// Copy
					doc.execCommand("Copy" ,false, empty);
					break;

				case 4:		// Paste
					doc.execCommand("Paste" ,false, empty);
					break;

				case 7:		// Bold
					doc.execCommand("Bold" ,false, empty);
					break;

				case 8:		// Italic
					doc.execCommand("Italic" ,false, empty);
					break;

				case 9:		// Underline
					doc.execCommand("Underline" ,false, empty);
					break;

				case 6:		// Undo
					doc.execCommand("Undo" ,false, empty);
					break;

				case 5:		// Redo
					doc.execCommand("Redo" ,false, empty);
					break;
			}

			if (e.Button.Text != "")
			{				
				doc.execCommand("FontSize" ,false, (object)e.Button.Text);
			}
		}

		private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try
			{
				object path = this.openFileDialog1.FileName;
				object missing = System.Reflection.Missing.Value;
				this.axWebBrowser1.Navigate2(ref path, ref missing, ref missing, ref missing, ref missing);		
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message, "Error");
				return;
			}
		}

        //private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        //{
        //    try
        //    {
        //        string filename = this.saveFileDialog1.FileName;
        //        StreamWriter file = new StreamWriter(filename, false, System.Text.Encoding.Unicode);
        //        mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)this.axWebBrowser1.Document;
        //        string str = "<body>" + doc.IHTMLDocument2_body.innerHTML + "</body>";
        //        file.Write(str);
        //        file.Close();
        //    }
        //    catch(Exception ex)
        //    {
        //        MessageBox.Show(ex.Message, "Error");
        //        return;
        //    }
        //}

		private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
            //try
            //{
            //    if (this.tabControl1.SelectedIndex == 1)	// Code View
            //    {
            //        mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)this.axWebBrowser1.Document;
            //        this.textBox1.Text = "<body>" + doc.IHTMLDocument2_body.innerHTML + "</body>";
            //    }
            //}
            //catch(Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Error");
            //    return;
            //}
		}

		private void linkLabel2_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start("mailto:shlomo.schwarcz@shefertech.com");				
		}

		private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start("http://www.shefertech.com");		
		}

        private void axWebBrowser1_Enter(object sender, EventArgs e)
        {

        }
	}
    public class DoubleRender : IAttributeRenderer
    { 
        public string ToString( object o,string fmt,System.Globalization.CultureInfo i)
        {
            Double d = (Double)o;
            if (fmt == null)
            {
                return string.Format("{0:F5}", d);
            }
            else
            {
                return string.Format("{0:" + fmt + "}", d);
            }
        }
    }
    //public class BasicFormatRender(Antlr4.StringTemplate.IAttributeRenderer
    //{
    //    public string ToString(object o, string fmt, System.Globalization.CultureInfo i)
    //    {
    //        Double d = (Double)o;
    //        return string.Format("{0:F5}", d);
    //    }
    //}
}
